<template>
  <div class="login">
    <div class="text">校园二手书交易管理系统后台</div>
    <div class="panel">
      <el-form :model="form" :rules="rules" label-width="auto">
        <el-form-item label="账号" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入账号"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input
            v-model="form.pwd"
            type="password"
            placeholder="请输入密码"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="submit">
        <el-button type="primary" @click="login">登陆</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        pwd: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 18, message: "请输入3至18个字符", trigger: "blur" }
        ],
        pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 18, message: "请输入3至18个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    login() {
      if (this.form.name == "" || this.form.pwd == "") {
        this.$message({
          message: "密码和账号不能为空",
          type: "error"
        });
      } else {
        this.$http
          .post("/login", this.form)
          .then(res => {
            // console.log(res);
            if (res.data != null) {
              this.$message({
                message: "登录成功",
                type: "success"
              });
              sessionStorage.setItem("loginname", res.data.loginname);
              sessionStorage.setItem("nickname", res.data.nickname);
              sessionStorage.setItem("loginpassword", res.data.loginpassword);
              sessionStorage.setItem("id", res.data.id);
              this.$router.push({ path: "/index" });
            } else {
              this.$message({
                message: "密码和账号不一致，请重新输入",
                type: "error"
              });
              this.form.name = null;
              this.form.pwd = null;
            }
          })
          .catch(function() {});
      }
    },
    reset() {
      this.form.name = null;
      this.form.pwd = null;
    }
  }
};
</script>

<style scoped="scoped">
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(to bottom, #f3eccf, #6fa5ab);
  color: rgb(79, 77, 77);
}
.login .panel {
  padding: 20px;
  width: 320px;
}
.login .text {
  text-align: center;
  margin-bottom: 30px;
  width: 100%;
  font-size: 2em;
  letter-spacing: 5px;
  font-weight: 600;
}
.submit {
  width: 100%;
  margin-top: 50px;
  display: flex;
  justify-content: center;
}
</style>